package com.zwr.sheller.shop.listener;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.zwr.sheller.core.common.BusinessException;
import com.zwr.sheller.core.common.WDMBusinessException;
import com.zwr.sheller.core.pojo.*;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.shop.mapper.GoodsMapper;
import com.zwr.sheller.shop.mapper.SkuMapper;
import com.zwr.sheller.shop.service.OrderService;
import com.zwr.sheller.shop.service.OrderSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Map;
import java.util.Set;

@Component
//@Slf4j
//@RabbitListener(queues = "interactive_queue")
@Transactional(rollbackFor = Throwable.class)
public class InteractiveListener {
  /*  @Autowired(required = false)
    private RedisTemplate redisTemplate;

   @Autowired(required = false)
   private OrderService orderService;
    @Autowired(required = false)
    private OrderSkuService orderSkuService;

    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    @Autowired(required = false)
    private SkuMapper skuMapper;



    @RabbitHandler
    public  void handler(String message) throws WDMBusinessException, BusinessException {

        System.out.println(message);
        Map<String,Object> data = JsonUtil.stringToObject(message, Map.class);
        //将信息转存到订单实体中并存到数据库
        OriginOrder order =JsonUtil.stringToObject(JsonUtil.objectToString(data.get("order")),OriginOrder.class) ;

        OriginOrder order1 = orderService.insertTSelective(order);
        System.out.println(order1);

        Map<String,Integer> skuIdWithNumber =(Map)data.get("skuIdWithNumber");
        Set<Map.Entry<String, Integer>> entries = skuIdWithNumber.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            OrderSku orderSku = new OrderSku(order1.getOrderId(),Long.valueOf(entry.getKey()),entry.getValue());

            orderSkuService.insertTSelective(orderSku);


            Sku sku = skuMapper.selectByPrimaryKey(entry.getKey());
            sku.setSkuStock(sku.getSkuStock()-entry.getValue());
            //增加对应的数据库中的sku库存
            orderService.updateSkuStock(sku);

            //增加商品销量
//                redisTemplate.opsForZSet().incrementScore("salesVolume:",redisSku.getGoodsId(),entry.getValue());
        }


    }*/
}
